using Microsoft.Win32;
using System;
using System.IO;
using System.Text;

namespace APP.HELPER.Utils.Net
{
	public class Attachment
	{
		private readonly string _contentType;
		private readonly string _encoding;
		private readonly string _fileName;
		private readonly string _filePath;
		private readonly long _size;
		public Attachment(string filePath)
		{
			this._filePath = filePath;
			this._encoding = "base64";
			if (this._filePath.Length > 0)
			{
				FileInfo fileInfo = new FileInfo(this._filePath);
				if (fileInfo.Exists)
				{
					this._fileName = fileInfo.Name;
					this._size = fileInfo.Length;
					this._contentType = Attachment.GetContentType(fileInfo.Extension);
				}
			}
		}
		public string GetMimeMessage()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("Content-Type: " + this._contentType + "; ");
			stringBuilder.Append("name=\"" + this._fileName + "\"\r\n");
			stringBuilder.Append("Content-Transfer-Encoding: " + this._encoding + "\r\n");
			stringBuilder.Append("Content-Disposition: attachment; ");
			stringBuilder.Append("filename=\"" + this._fileName + "\"\r\n\r\n");
			stringBuilder.Append(MailEncoder.GetBase64String(this._filePath));
			return stringBuilder.ToString();
		}
		private static string GetContentType(string fileExtension)
		{
			string result;
			try
			{
				RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(fileExtension);
				string text = registryKey.GetValue("Content Type").ToString();
				result = ((text != "" && text != null) ? text : "application/octet-stream");
			}
			catch (Exception)
			{
				result = "application/octet-stream";
			}
			return result;
		}
	}
}
